package com.kelvin.paopao.widgets;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.kelvin.paopao.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/5/7.
 */

public class CustomToolbar extends FrameLayout {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    private Context mContext;

    private String title;


    public CustomToolbar(Context context) {
        this(context, null);
    }

    public CustomToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init(attrs);
    }

    private void init(AttributeSet attrs) {


        View.inflate(mContext, R.layout.layout_custom_toolbar, this);
        ButterKnife.bind(this);

        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.CustomToolbar);
        try {
            title = a.getString(R.styleable.CustomToolbar_title);
            if (!TextUtils.isEmpty(title)) {
                setTitle(title);
            }
        } finally {
            a.recycle();
        }


    }

    /**
     * 返回按钮
     *
     * @param activity
     */
    public void showBackIcon(final Activity activity) {
        toolbar.setNavigationIcon(R.drawable.arrow_left_white);
        toolbar.setNavigationOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    /**
     * 设置menu
     *
     * @param menuId
     */
    public void inflateMenu(int menuId) {
        toolbar.inflateMenu(menuId);
    }

    /**
     * 設置監聽器
     *
     * @param listener
     */
    public void setMenuOnClick(Toolbar.OnMenuItemClickListener listener) {
        toolbar.setOnMenuItemClickListener(listener);
    }

}
